<?php

namespace App\Http\Controllers\Site\Custom\Provider;

use App\B2BProject\Custom\Business\Issue;
use App\B2BProject\Custom\Member;
use App\B2BProject\Site\HelperControllerBuildTrait;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class IssueController extends Controller
{
    use HelperControllerBuildTrait;

    public function getIndex(Request $request)
    {
        if ($request->ajax()) {
            $request->query->set('page', $request->query('page', $request->page));
            $pageSize = $request->query('pageSize', $request->pageSize);

            $ids    = \Auth::user()->store->goods()->lists('id');
            $issues = Issue::whereIn('goods_id', $ids);

            switch ($request->query('type')) {
                case 2:
                    $issues->withoutReplied();
                    break;
                case 3:
                    $issues->isReplied();
            }

            $issues = $issues->orderBy('created_at', 'desc')->paginate($pageSize);

            $data = [
                'data'           => [],
                'pagingConfData' => [
                    'page'     => $issues->currentPage(),
                    'pageSize' => $pageSize,
                    'total'    => $issues->total(),
                ],
            ];

            foreach ($issues as $issue) {
                $data['data'][] = [
                    'id'      => $issue->id,
                    'img'     => $issue->goods->cover(),
                    'proname' => $issue->goods->name,
                    'ask'     => $issue->content,
                    'reply'   => $issue->reply ? $issue->reply->content : '',
                    'time'    => date('Y-m-d H:i:s', strtotime($issue->created_at)),
                    'isreply' => $issue->replied,
                ];
            }

            return response()->json($data);
        }

        return view('host/custom/provider/issue/index');
    }

    public function postIndex(Request $request)
    {
        return \DB::transaction(function () use ($request) {
            $validator = \Validator::make($request->all(), [
                'id'    => 'required|exists:issues,id',
                'replay' => 'required',
            ]);

            if ($validator->fails()) {
                return response('Forbidden', 403);
            }
            
            $issue = Issue::findOrFail($request->id);
            $issue->createReply($request->input('replay'));

            $request->pageSize = 10;
            $request->page = 1;

            return $this->getIndex($request);
        });
    }

    public function deleteIndex(Request $request)
    {
        return \DB::transaction(function () use ($request) {
            $request->pageSize = 10;
            $request->page = 1;

            $validator = \Validator::make($request->all(), [
                'id'    => 'required|exists:issues,id',
            ]);

            if ($validator->fails()) {
                return response('Forbidden', 403);
            }

            $issue = Issue::findOrFail($request->id);

            $issue->reply()->delete();
            $issue->delete();

            return $this->getIndex($request);
        });
    }
}
